package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * 攻略明细
 */
@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyThemeService strategyThemeService;


    // 攻略明细显示
    // 这里需要的是分页的数据
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyQuery qo){
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page",page);
        return "/strategy/list";
    }


    
    /**
     * 新增和修改
     * 判断对象中是否有Id来确定执行新增还是修改方法
     * @param strategy
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        if (StringUtils.hasLength(strategy.getId())){
            strategyService.update(strategy);
        }else{
            strategyService.save(strategy);
        }
        return JsonResult.success();
    }


    @RequestMapping("/input")
    public String input(Model model,String id){
        if (StringUtils.hasLength(id) ){

            Strategy strategy = strategyService.get(id);

            model.addAttribute("strategy",strategy);

        }
        List<CatalogVO> catalogs = strategyCatalogService.groupList();
        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("catalogs",catalogs);
        model.addAttribute("themes",themes);

        return "/strategy/input";
    }


    /**
     * 根据传上来的id删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        if (StringUtils.hasLength(id)) {
            strategyService.delete(id);
        }
        return JsonResult.success();
    }


    /**
     * 下架和发布
     * @param state
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/changeState")
    public Object changeState(Integer state,String id){
        Strategy strategy = strategyService.get(id);
        strategy.setState(state);
        strategyService.update(strategy);

        return JsonResult.success();
    }







}
